<template>
	<el-container>
		<el-header>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="wechat_token"></el-input>
					<el-input v-model="wechat_url"></el-input>
					<el-button type="primary" icon="el-icon-edit" @click="wechatEdit">保存信息</el-button>
				</div>
			</div>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button v-if="!healthz" type="primary" icon="el-icon-plus" @click="table_show">登录</el-button>
				<el-button v-if="!healthz" type="primary" icon="el-icon-plus" @click="reload">扫码后点刷新</el-button>
				<el-button v-if="healthz">{{contact}}</el-button>
			</div>
		</el-header>
		<el-header>
			<div class="left-panel">
				
				<el-input v-model="add_contact"></el-input>
				<el-button type="primary" icon="el-icon-plus" @click="addContact">添加联系人</el-button>
			</div>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-space wrap>
					<el-tag v-for="(item,index) in contacts" :key="index" closable @close="delContact(index)">{{item}}</el-tag>
				</el-space>
			</div>
		</el-header>
		<el-main class="nopadding">
			
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

	<el-drawer v-model="dialog.info" :size="800" title="详细" direction="rtl" destroy-on-close>
		<info ref="infoDialog"></info>
	</el-drawer>

</template>

<script>
	import saveDialog from './save'
	import info from './info'

	export default {
		name: 'listCrud',
		components: {
			saveDialog,
			info
		},
		data() {
			return {
				wechat_token: "",
				wechat_url: "",
				logurl: "",
				add_contact: "",
				contacts: [],
				healthz: false,
				contact: '',
				dialog:{
					save: false,
					info: false
				},
				list: {
					apiObj: this.$API.demo.list
				},
				selection: []
			}
		},
		mounted() {
			this.checkHealthz()
		},
		methods: {
			async checkHealthz() {
				var res = await this.$API.wechat.checkHealthz.get();
				if (res.code == 200) {
					this.healthz = true
					this.contact = res.data.message
				}
				this.logurl = res.logurl
				this.wechat_token = res.wechat_token
				this.wechat_url = res.wechat_url
				this.contacts = res.contacts
			},
			reload() {
				window.location.reload()
			},
			async wechatEdit() {
				var res = await this.$API.wechat.saveWechat.post({wechat_token: this.wechat_token, wechat_url: this.wechat_url})
				if (res.code == 200) {
					this.$message.success("成功")
				} else {
					this.$message.error(res.msg)
				}
			},
			addContact() {
				if (this.add_contact == '') {
					return false
				}
				this.contacts.push(this.add_contact)
				this.saveContacts()
			},
			delContact(index) {
				this.contacts.splice(index, 1)
				this.saveContacts()
			},
			async saveContacts() {
				var contacts_str = this.contacts.join(',');
				var res = await this.$API.wechat.saveContacts.post({contacts_str: contacts_str});

				if (res.code == 200) {
					this.$message.success("成功")
				} else {
					this.$message.error(res.msg)
				}
			},
			//窗口新增
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//窗口编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//页面新增
			addPage(){
				this.$router.push({
					path: '/template/list/crud/detail',
				})
			},
			//页面编辑
			table_edit_page(row){
				this.$router.push({
					path: '/template/list/crud/detail',
					query: {
						id: row.id
					}
				})
			},
			//登录
			table_show(){
				window.open(this.logurl, '_blank')
			},
			//删除明细
			async table_del(row, index){
				var reqData = {id: row.id}
				var res = await this.$API.demo.post.post(reqData);
				if(res.code == 200){
					this.$refs.table.removeIndex(index)
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				var confirmRes = await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '删除',
					confirmButtonClass: 'el-button--danger'
				}).catch(() => {})

				if(!confirmRes){
					return false
				}

				var ids = this.selection.map(v => v.id)
				this.$refs.table.removeKeys(ids)
				this.$message.success("操作成功")

			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				//为了减少网络请求，直接变更表格内存数据
				if(mode=='add'){
					this.$refs.table.unshiftRow(data)
				}else if(mode=='edit'){
					this.$refs.table.updateKey(data)
				}

				//当然也可以暴力的直接刷新表格
				// this.$refs.table.refresh()
			}
		}
	}
</script>

<style>
</style>
